package com.aerozhonghuan.logisticsmall.modules.user.logic;

import android.content.Context;
import android.text.TextUtils;

import com.aerozhonghuan.logisticsmall.framework.base.URLs;
import com.aerozhonghuan.logisticsmall.modules.user.beans.UserBean;
import com.aerozhonghuan.logisticsmall.modules.user.beans.UserInfo;
import com.aerozhonghuan.logisticsmall.utils.MD5;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;
import com.aerozhonghuan.oknet2.RequestBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

/**
 * 描述 登录相关
 *
 * @author zhangyonghui
 * 创建日期 2019-05-17
 */
public class LoginLogic {
    private static final String TAG = "LoginLogic";

    /**
     * 提交设备信息,没有deviceId的情况下获取
     *
     * @param context
     * @param devName
     * @param devResolution
     * @param devOs
     * @param devOsVersion
     * @param devDetails
     */
    public static void pushDevice(final Context context, String devName, String devResolution, String devOs, String devOsVersion, String devDetails, final OnPushDeviceCallback onPushDeviceCallback) {
        RequestBuilder.with(context)
                .URL(URLs.SERVER_PUSH_DEVICE)
                .para("dev_name", devName)
                .para("dev_resolution", devResolution)
                .para("dev_os", devOs)
                .para("dev_os_version", devOsVersion)
                .para("dev_details", devDetails)
                .onSuccess(new CommonCallback<String>() {
                    @Override
                    public void onSuccess(String messsageBodyObject, CommonMessage commonMessage, String allResponseString) {
                        try {
                            JSONObject jsonObject = new JSONObject(messsageBodyObject);
                            String devId = jsonObject.getString("devId");
                            String uuid = jsonObject.getString("uuid");
                            if (!TextUtils.isEmpty(devId)) {
                                UserInfoManager.saveDeviceId(devId);
                            }
                            if (!TextUtils.isEmpty(uuid)) {
                                UserInfoManager.saveUuid(uuid);
                            }
                            if (!TextUtils.isEmpty(devId) && !TextUtils.isEmpty(uuid)) {
                                onPushDeviceCallback.onSuccess(devId, uuid);
                            } else {
                                onPushDeviceCallback.onFailure();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            onPushDeviceCallback.onFailure();
                        }
                    }

                    @Override
                    public boolean onFailure(int httpCode, Exception exception, CommonMessage responseMessage, String allResponseString) {
                        onPushDeviceCallback.onFailure();
                        return true;
                    }
                }).excute();
    }

    /**
     * 更新deviceId 本地存在deviceId但是没有uuid情况下获取uuid
     *
     * @param context
     * @param devId
     */
    public static void updateDevice(Context context, String devId, final OnUpdateDeviceCallback onUpdateDeviceCallback) {
        RequestBuilder.with(context)
                .URL(URLs.SERVER_UPDATE_DEVICE)
                .para("devId", devId)
                .onSuccess(new CommonCallback<String>() {
                    @Override
                    public void onSuccess(String messsageBodyObject, CommonMessage commonMessage, String allResponseString) {
                        try {
                            JSONObject jsonObject = new JSONObject(messsageBodyObject);
                            String uuid = jsonObject.getString("uuid");
                            if (!TextUtils.isEmpty(uuid)) {
                                UserInfoManager.saveUuid(uuid);
                                onUpdateDeviceCallback.onSuccess(uuid);
                            } else {
                                onUpdateDeviceCallback.onFailure();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            onUpdateDeviceCallback.onFailure();
                        }
                    }

                    @Override
                    public boolean onFailure(int httpCode, Exception exception, CommonMessage responseMessage, String allResponseString) {
                        onUpdateDeviceCallback.onFailure();
                        return true;
                    }
                }).excute();
    }

    /**
     * 检查token是否有效,如果有效直接进入主页,如果无效需要重新登录
     *  @param context
     * @param progressDialogIndicator
     * @param token
     * @param devId
     * @param uuid
     * @param onCheckTokenCallback
     */
    public static void checkToken(Context context, ProgressDialogIndicator progressDialogIndicator, String token, String devId, String uuid, final OnCheckTokenCallback onCheckTokenCallback) {
        TypeToken<UserInfo> type = new TypeToken<UserInfo>() {
        };
        CommonCallback<UserInfo> callback = new CommonCallback<UserInfo>() {
            @Override
            public void onSuccess(UserInfo userInfo, CommonMessage commonMessage, String allResponseString) {
                if (userInfo != null) {
                    UserInfoManager.saveUserBaseInfo(userInfo);
                    onCheckTokenCallback.onSuccess(userInfo);
                } else {
                    onCheckTokenCallback.onFailure();
                }
            }

            @Override
            public boolean onFailure(int httpCode, Exception exception, CommonMessage responseMessage, String allResponseString) {
                onCheckTokenCallback.onFailure();
                return true;
            }
        };
        callback.setResponseType(type);
        RequestBuilder.with(context)
                .URL(URLs.SERVER_CHECK_TOKEN)
                .progress(progressDialogIndicator)
                .para("token", token)
                .para("devId", devId)
                .para("uuid", uuid)
                .onSuccess(callback).excute();
    }

    /**
     * 检查token是否有效,如果有效直接进入主页,如果无效需要重新登录
     *
     * @param context
     * @param dialog
     * @param onLoginCallback
     */
    public static void login(Context context, String token, String username, String password, ProgressDialogIndicator dialog, final OnLoginCallback onLoginCallback) {
        String psw = MD5.GetMD5Code(String.format("%s%s", MD5.GetMD5Code(String.format("%s%s", username, password)), token));
        TypeToken<UserBean> type = new TypeToken<UserBean>() {
        };
        CommonCallback<UserBean> callback = new CommonCallback<UserBean>() {
            @Override
            public void onSuccess(UserBean userBean, CommonMessage commonMessage, String allResponseString) {
                if (userBean != null) {
                    UserInfoManager.saveUserDetailInfo(userBean);
                    onLoginCallback.onSuccess(userBean);
                } else {
                    onLoginCallback.onFailure(allResponseString);
                }
            }

            @Override
            public boolean onFailure(int httpCode, Exception exception, CommonMessage responseMessage, String allResponseString) {
                onLoginCallback.onFailure(allResponseString);
                return true;
            }
        };
        callback.setResponseType(type);
        RequestBuilder.with(context)
                .URL(URLs.SERVER_LOGIN)
                .progress(dialog)
                .para("username", username)
                .para("password", psw)
                .onSuccess(callback).excute();

    }

    /**
     * 发送验证码
     *
     * @param context
     * @param dialog
     * @param onSendSMSCallback
     */
    public static void sendSMS(Context context, String mobile, String smsMold, ProgressDialogIndicator dialog, final OnSendSMSCallback onSendSMSCallback) {
        RequestBuilder.with(context)
                .URL(URLs.SERVER_SEND_SMS)
                .progress(dialog)
                .para("mobile", mobile)
                .para("smsMold", smsMold)
                .onSuccess(new CommonCallback<String>() {
                    @Override
                    public void onSuccess(String response, CommonMessage commonMessage, String allResponseString) {
                        onSendSMSCallback.onSuccess();
                    }

                    @Override
                    public boolean onFailure(int httpCode, Exception exception, CommonMessage responseMessage, String allResponseString) {
                        onSendSMSCallback.onFailure();
                        return super.onFailure(httpCode, exception, responseMessage, allResponseString);
                    }
                }).excute();

    }

    /**
     * 获取用户信息
     *
     * @param context
     * @param callback
     */
    public static void getUserInfo(Context context, CommonCallback<UserBean> callback) {
        TypeToken<UserBean> type = new TypeToken<UserBean>() {
        };
        callback.setResponseType(type);
        RequestBuilder.with(context)
                .URL(URLs.SERVER_USER_INFO)
                .onSuccess(callback).excute();
    }

    /**
     * 提交device回调
     */
    public interface OnPushDeviceCallback {
        void onSuccess(String devId, String uuid);

        void onFailure();
    }

    /**
     * 更新deviceId回调
     */
    public interface OnUpdateDeviceCallback {
        void onSuccess(String uuid);

        void onFailure();
    }

    /**
     * 校验token回调
     */
    public interface OnCheckTokenCallback {
        void onSuccess(UserInfo userInfo);

        void onFailure();
    }

    /**
     * 登录回调
     */
    public interface OnLoginCallback {
        void onSuccess(UserBean userBean);

        void onFailure(String allResponseString);
    }

    /**
     * 登录回调
     */
    public interface OnSendSMSCallback {
        void onSuccess();

        void onFailure();
    }
}
